package com.sunny.dao.mongo;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Sunny on 2016/4/19.
 */
@Service
public class MongoDao extends BaseDao<DBObject> {

    public static final String collectionName = "prism_vb_data2";

    @Override
    protected Class<DBObject> getEntityClass(){
        return DBObject.class;
    }

    public DBObject insert(DBObject dbObject){
        if(!mongoTemplate.collectionExists(collectionName)){
            mongoTemplate.createCollection(collectionName);
        }
        mongoTemplate.insert(dbObject,MongoDao.collectionName);
        return dbObject;
    }

    /**
     * 通过条件查询实体(集合)
     *
     * @param query
     */
    public List<DBObject> find(Query query) {
        return mongoTemplate.find(query, this.getEntityClass(),collectionName);
    }

    public List<DBObject> findAll() {
        return  mongoTemplate.findAll(this.getEntityClass(),collectionName);
    }

    /**
     *
     * @param query 查询条件
     * @param skip 开始记录
     * @param count  查询条数
     * @return
     */
    public List<DBObject> find(Query query,int skip,int count){
        return mongoTemplate.find(query.skip(skip).limit(count), this.getEntityClass(),collectionName);
    }


    public DBCursor findByFieldsDBCursor(int skip,int limit) throws Exception {
        DBObject fieldsObject = new BasicDBObject();
        fieldsObject.put("order_id",1);
        fieldsObject.put("credit_id",1);
        fieldsObject.put("credit_front",1);
        fieldsObject.put("credit_back",1);
        fieldsObject.put("personal_pic",1);
        fieldsObject.put("student_id_pic",1);
        fieldsObject.put("fee_list_pic",1);
        fieldsObject.put("user_info_pic",1);
        fieldsObject.put("dorm_pic",1);
        fieldsObject.put("school_roll_pic",1);
        fieldsObject.put("contract_front",1);
        fieldsObject.put("chis_school_rol_pic",1);//学信网照片
        fieldsObject.put("intranet_school_rol_pic",1);//
        DBCursor cursor = null;
        try{
            cursor   = mongoTemplate.getCollection(collectionName).find(null,fieldsObject).sort(new BasicDBObject("_id",-1)).skip(skip).limit(limit);
        }catch (Exception e){
            throw e;
        }
        return cursor;
    }


    public List<DBObject> findByFields(int skip,int limit){
        BasicDBObject fieldsObject=new BasicDBObject();
        fieldsObject.put("order_id",1);
        fieldsObject.put("credit_id",1);
        fieldsObject.put("credit_front",1);
        fieldsObject.put("credit_back",1);
        fieldsObject.put("personal_pic",1);
        fieldsObject.put("student_id_pic",1);
        fieldsObject.put("fee_list_pic",1);
        fieldsObject.put("user_info_pic",1);
        fieldsObject.put("dorm_pic",1);
        fieldsObject.put("school_roll_pic",1);
        fieldsObject.put("contract_front",1);
        fieldsObject.put("chis_school_rol_pic",1);//学信网照片
        fieldsObject.put("intranet_school_rol_pic",1);//
        BasicQuery basicQuery = new BasicQuery( new BasicDBObject(),fieldsObject);

        return mongoTemplate.find(basicQuery.skip(skip).limit(limit),this.getEntityClass(),collectionName);
    }

}
